package com.his.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.his.entity.Department;
import com.his.entity.Drug;
import com.his.entity.ResponseResult;
import com.his.mapper.DrugMapper;
import com.his.service.IDrugService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 药品项目表（包括了重要、中成药、草药） 服务实现类
 * </p>
 *
 * @author Demoon Liu
 * @since 2022-02-20
 */
@Service
public class DrugServiceImpl extends ServiceImpl<DrugMapper, Drug> implements IDrugService {

    @Override
    public ResponseResult myInsert(Drug drug) {
        Long count = 1L;
        Integer code = 0;
        String message = "";

        try {
            Drug repeat = this.getOne(new QueryWrapper<Drug>().eq("name", drug.getName()));
            if (null != repeat) throw new Exception("已存在相同的药品");

            count = this.count();
            drug.setId(count+1);
            drug.setStatus(1);
            drug.setCreatedate(LocalDateTime.now());
            if (!this.save(drug)) {
                throw new Exception();
            }

            message = "插入成功";
            code = 201;
        }
        catch (Exception err) {
            message = "插入失败";
            code = 400;
        }

        return new ResponseResult(code, message, drug);
    }

    @Override
    public ResponseResult myUpdate(Drug drug) {
        Integer code = 0;
        String message = "";

        try {

            if (!this.updateById(drug)) {
                throw new Exception();
            }

            message = "修改成功";
            code = 201;
        }
        catch (Exception err) {
            message = "修改失败";
            code = 400;
        }

        return new ResponseResult(code, message, drug);
    }

    @Override
    public ResponseResult mySearchAll() {
        Integer code = 0;
        String message = "";
        IPage<Drug> page = new Page<Drug>(1, 10);
        IPage<Drug> result = null;

        try {
            result = this.page(page);
            message = "查询成功";
            code = 201;

        }
        catch (Exception err) {
            message = "查询失败";
            code = 400;
        }

        return new ResponseResult(code, message, result);
    }

    @Override
    public ResponseResult myFuzzySearch(String str) {
        Integer code = 0;
        String message = "";
        IPage<Drug> page = new Page<Drug>(1, 10);
        IPage<Drug> result = null;

        try {
            result = this.page(page, new QueryWrapper<Drug>().like("code", str)
                    .or().like("name", str)
                    .or().like("mnemoniccode", str)
            );
            message = "模糊查询成功";
            code = 201;



        }
        catch (Exception err) {
            message = "模糊查询失败";
            code = 400;
        }

        return new ResponseResult(code, message, result);
    }

    @Override
    public ResponseResult myDelete(Drug drug) {
        Integer code = 0;
        String message = "";

        try {
            if (!this.removeById(drug)) {
                throw new Exception();
            }
            message = "删除成功";
            code = 201;



        }
        catch (Exception err) {
            message = "删除失败";
            code = 400;
        }

        return new ResponseResult(code, message, null);
    }
}
